package com.hethongviet.job.helpers.database;

import com.hethongviet.job.models.User;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBLoginHelper extends SQLiteOpenHelper
{
	public static final String TABLE_LOGIN = "infos";
	public static final String COLUMN_ID = "_id";
	public static final String COLUMN_USERNAME = "username";
	public static final String COLUMN_PASSWORD = "password";
	public static final String COLUMN_ACTIVATION_CODE = "activation_code";
	public static final String COLUMN_AUTO_LOGIN = "auto_login";

	private static final String DATABASE_NAME = "login.db";
	private static final int DATABASE_VERSION = 1;

	// Database creation SQL statement
	private static final String DATABASE_CREATE = "create table if not exists"
			+ TABLE_LOGIN + "( " + COLUMN_ID
			+ " integer primary key autoincrement, " + COLUMN_USERNAME
			+ " text not null, " + COLUMN_PASSWORD + " text not null, "
			+ COLUMN_ACTIVATION_CODE + " text not null, " + COLUMN_AUTO_LOGIN
			+ " integer not null" + ");";

	private String[] allColumns = new String[] { COLUMN_ID, COLUMN_USERNAME,
			COLUMN_PASSWORD, COLUMN_ACTIVATION_CODE, COLUMN_AUTO_LOGIN };

	public DBLoginHelper(Context context)
	{
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase sqLiteDatabase)
	{
		sqLiteDatabase.execSQL(DATABASE_CREATE);
		sqLiteDatabase.close();
	}

	@Override
	public void onUpgrade(SQLiteDatabase sqLiteDatabase, int v1, int v2)
	{
		sqLiteDatabase.execSQL("drop table if exists " + TABLE_LOGIN);
		onCreate(sqLiteDatabase);
	}

	public void insert(User loginInfo)
	{
		SQLiteDatabase database = getWritableDatabase();
		/* Get and check if exits this data */
		Cursor cursor = database.query(TABLE_LOGIN, allColumns, COLUMN_ID + "="
				+ loginInfo.getId(), null, null, null, null);
		ContentValues values = new ContentValues();
		if (cursor.getCount() > 0)
		{
			cursor.moveToFirst();
			String activationCode = cursor.getString(cursor.getColumnIndex(COLUMN_ACTIVATION_CODE));
			String username = cursor.getString(cursor.getColumnIndex(COLUMN_USERNAME));
			String password = cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD));
			int autoLogin = cursor.getInt(cursor.getColumnIndex(COLUMN_AUTO_LOGIN));
			boolean isAutoLogin = autoLogin == 0 ? false : true;
			if (!activationCode.equals(loginInfo.getActivationCode())
					|| !username.equals(loginInfo.getUsername())
					|| !password.equals(loginInfo.getPassword())
					|| (isAutoLogin != loginInfo.isAutoLogin()))
			{
				/* Update to database */
				values.put(COLUMN_USERNAME, loginInfo.getUsername());
				values.put(COLUMN_PASSWORD, loginInfo.getPassword());
				values.put(COLUMN_AUTO_LOGIN, loginInfo.isAutoLogin() ? 1 : 0);
				database.update(TABLE_LOGIN, values, COLUMN_ID + "="
						+ loginInfo.getId(), null);
			}
		}
		else
		{
			/* Insert new */
			values.put(COLUMN_USERNAME, loginInfo.getUsername());
			values.put(COLUMN_PASSWORD, loginInfo.getPassword());
			values.put(COLUMN_AUTO_LOGIN, loginInfo.isAutoLogin() ? 1 : 0);
			database.insert(TABLE_LOGIN, null, values);
		}
		database.close();
	}

	public void delete(User loginInfo)
	{
		SQLiteDatabase database = getWritableDatabase();
		database.delete(TABLE_LOGIN, COLUMN_ID + " = " + loginInfo.getId(),
				null);
		database.close();
	}

	public User getLoginInfo(String username)
	{
		SQLiteDatabase database = getWritableDatabase();
		User loginInfo = null;
		Cursor cursor = database.query(TABLE_LOGIN, allColumns, COLUMN_USERNAME
				+ " = " + username, null, null, null, null);
		if (cursor.getCount() > 0)
		{
			loginInfo = new User();
			loginInfo.setActivationCode(cursor.getString(cursor.getColumnIndex(COLUMN_ACTIVATION_CODE)));
			loginInfo.setUsername(cursor.getString(cursor.getColumnIndex(COLUMN_USERNAME)));
			loginInfo.setPassword(cursor.getString(cursor.getColumnIndex(COLUMN_PASSWORD)));
			int autoLogin = cursor.getInt(cursor.getColumnIndex(COLUMN_AUTO_LOGIN));
			loginInfo.setAutoLogin(autoLogin == 0 ? false : true);
		}
		database.close();
		return loginInfo;
	}
}
